// components/common/multi-select/index.js
const componentBehavior = require('../../../behaviors/component');
const { COMPONENT_OPTIONS } = require('../../../constants/common');
const { arr2Obj } = require('../../../common/utils');

Component({
    behaviors: [componentBehavior],
    options: {
        ...COMPONENT_OPTIONS,
    },
    properties: {
        show: {
            type: Boolean,
            value: false,
        },
        title: {
            type: String,
            value: '请选择',
        },
        options: {
            type: Array,
            value: [],
        },
        selects: {
            type: Array,
            value: [],
        },
    },
    observers: {
        'selects': function (val) {
            val && this.setData({ selectsMap: arr2Obj(val) });
        },
    },
    data: {
        selectsMap: {},
    },
    methods: {
        handleClose(e) {
            this.setData({
                show: false,
            });
            // this.triggerEvent('close');
        },
        handleChange(e) {
            const { value } = e.currentTarget.dataset;
            const { selectsMap } = this.data;
            if (selectsMap[value]) {
                delete selectsMap[value];
            } else {
                selectsMap[value] = value;
            }
            const selects = Object.keys(selectsMap);
            this.triggerEvent('change', selects);
        },
    }
})


